﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using Web.Model;

namespace Web.Handler
{
    /// <summary>
    /// MenuHandler 的摘要说明
    /// </summary>
    public class MenuHandler : IHttpHandler,IRequiresSessionState
    {
       
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            HttpResponse Response = context.Response;
            HttpRequest Request = context.Request;
            HttpServerUtility Server = context.Server;


            string action = Request["action"];
            if (action == "getmenus")
            {
                StringBuilder sbHtml = new StringBuilder();
                //所有顶部菜单
                var lstAllMenus = GetAllMenuTree();

                for (int j = 0; j < lstAllMenus.Count; j++)
                {
                    var menu = lstAllMenus[j];
                    sbHtml.AppendFormat("<li {0} >", menu.LIClassName);
                    sbHtml.AppendFormat("<a href='javascript:;' {0} {1}>", menu.AClassName, menu.ADataToggle);
                    sbHtml.AppendFormat("<i {0} />", menu.IClassName);
                    sbHtml.AppendFormat("{0}{1}", menu.Title, menu.TitleNextHtml);
                    sbHtml.AppendFormat("</a>");
                    sbHtml.Append(menu.ANextHtml);//组装 下拉或树状 子菜单
                    sbHtml.Append("</li>");
                }

                string topMenuHtml = sbHtml.ToString();
                string respJson = JsonHelper.ObjectToJson(Retful.CreateSuccess(topMenuHtml));
                Response.Write(respJson);
            }
        }

        /// <summary>
        /// 所有菜单
        /// </summary>
        /// <returns></returns>
        List<MenuItem> GetAllMenuTree()
        {
            var lstMenu = new List<MenuItem>()
            {
                //pid =-1 顶级菜单
                new MenuItem(title:"客户管理",isDefActive:1,id:"0"),
                new MenuItem(title:"内部办公",isContainsDropDownMenu:1,id:"1"),  

                new MenuItem(title:"联系人管理",id:"3"),
                new MenuItem(title:"产品管理"),
                new MenuItem(title:"售前管理"),
                new MenuItem(title:"销售管理"),
                new MenuItem(title:"服务管理"),
                new MenuItem(title:"合同管理"),
                new MenuItem(title:"财务管理"),
                new MenuItem(title:"人事管理"),
                new MenuItem(title:"项目管理")
            };
            return lstMenu;
        }

       
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}